"""
Created on 28-Oct-2009

@author: dexen
"""

# TODO: have to synch the methods
class NamingManager(object):
    def __init__(self, prefix):
        self.prefix = prefix
        self.cnt = 1 # shows the current available no
        self.released = {}
        self.allocated = {}

    def _generate_name(self, cnt):
        return self.prefix + str(cnt)
    
    def get_name(self):
        if len(self.released) == 0:
            name = self._generate_name(self.cnt)
            self.cnt += 1
            self.allocated[name] = None
        else:
            name = self.released.keys()[0]
            assert not self.allocated.has_key(name)
            self.allocated[name] = None
            del self.released[name]
        return name
    
    def release_name(self, name):
        assert self.allocated.has_key(name)
        assert not self.released.has_key(name)
        del self.allocated[name]
        self.released[name] = None

